package com.ruoyi.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.domain.ParkingCardSpec;
import com.ruoyi.domain.ParkingInfo;
import com.ruoyi.mapper.ParkingCardSpecMapper;
import com.ruoyi.mapper.ParkingInfoMapper;
import com.ruoyi.service.IParkingCardSpecService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * 停车卡配置Service业务层处理
 * 
 * @author dongdong
 * @date 2025-09-09
 */
@Service
public class ParkingCardSpecServiceImpl implements IParkingCardSpecService 
{
    @Resource
    private ParkingCardSpecMapper parkingCardSpecMapper;
    @Resource
    private ParkingInfoMapper parkingInfoMapper;

    /**
     * 查询停车卡配置
     * 
     * @param id 停车卡配置主键
     * @return 停车卡配置
     */
    @Override
    public ParkingCardSpec selectParkingCardSpecById(Long id)
    {
        return parkingCardSpecMapper.selectParkingCardSpecById(id);
    }

    /**
     * 查询停车卡配置列表
     * 
     * @param parkingCardSpec 停车卡配置
     * @return 停车卡配置
     */
    @Override
    @DataScope(deptAlias = "pcs")
    public List<ParkingCardSpec> selectParkingCardSpecList(ParkingCardSpec parkingCardSpec)
    {
        return parkingCardSpecMapper.selectParkingCardSpecList(parkingCardSpec);
    }

    /**
     * 新增停车卡配置
     * 
     * @param parkingCardSpec 停车卡配置
     * @return 结果
     */
    @Override
    public int insertParkingCardSpec(ParkingCardSpec parkingCardSpec)
    {
        ParkingInfo parkingInfo = parkingInfoMapper.selectParkingInfoById(parkingCardSpec.getParkId());
        Optional.ofNullable(parkingInfo).orElseThrow(() -> new RuntimeException("无效的停车场"));
        parkingCardSpec.setCreateBy(getUsername());
        parkingCardSpec.setCreateTime(DateUtils.getNowDate());
        parkingCardSpec.setDeptId(parkingInfo.getDeptId());
        return parkingCardSpecMapper.insertParkingCardSpec(parkingCardSpec);
    }

    /**
     * 修改停车卡配置
     * 
     * @param parkingCardSpec 停车卡配置
     * @return 结果
     */
    @Override
    public int updateParkingCardSpec(ParkingCardSpec parkingCardSpec)
    {
        parkingCardSpec.setUpdateBy(getUsername());
        parkingCardSpec.setUpdateTime(DateUtils.getNowDate());
        return parkingCardSpecMapper.updateParkingCardSpec(parkingCardSpec);
    }

    /**
     * 批量删除停车卡配置
     * 
     * @param ids 需要删除的停车卡配置主键
     * @return 结果
     */
    @Override
    public int deleteParkingCardSpecByIds(Long[] ids)
    {
        return parkingCardSpecMapper.deleteParkingCardSpecByIds(ids);
    }

    /**
     * 删除停车卡配置信息
     * 
     * @param id 停车卡配置主键
     * @return 结果
     */
    @Override
    public int deleteParkingCardSpecById(Long id)
    {
        return parkingCardSpecMapper.deleteParkingCardSpecById(id);
    }
}
